package com.definity.toolkit.util;

import java.util.Date;
import java.util.Locale;

import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

public class DateUtils {

	private DateUtils() {

	}
	
	public static String getPeriod(Date date) {
		return getPeriod(date, null);
	}
	
	public static String getPeriod(Date date, Date finalDate) {
		if (date == null)
			throw new IllegalArgumentException("date");
		if (finalDate == null)
			finalDate = new Date();
		
		Period period = new Period(date.getTime(), finalDate.getTime());
		
		PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
		builder.appendYears().appendSuffix(" ano, ", " anos, ").appendSeparator(", ", " e ");
		builder.appendMonths().appendSuffix(" mês, ", " meses, ").appendSeparator(", ", " e ");
		if (period.getMonths() == 0)
			builder.appendWeeks().appendSuffix(" semana, ", " semanas, ").appendSeparator(", ", " e ");
		builder.appendDays().appendSuffix(" dia", " dias").appendSeparator(", ", " e ");
		if (period.getWeeks() == 0)
			builder.appendHours().appendSuffix(" hora", " horas").appendSeparator(", ", " e ");
		if (period.getDays() == 0)
			builder.appendMinutes().appendSuffix(" minuto", " minutos").appendSeparator(", ", " e ");
		if (period.getMinutes() == 0)
			builder.appendSeconds().appendSuffix(" segundo", " segundos");
		PeriodFormatter formatter = builder.toFormatter().withLocale(Locale.getDefault());
		return formatter.print(period).trim();
	}
	
	public static Double getHours(Date date, Date horaAtual){
		Period period = new Period(horaAtual.getTime(), date.getTime());
		
		int hours = period.getHours();
		int minutes = period.getMinutes();
		int seconds = period.getSeconds();
		
		return  (double) (hours + (minutes / 60.0) + (seconds / 3600.0));
		
	}
	
}
